package br.com.siscov.model.login.bo;

import java.sql.Connection;

import br.com.siscov.exceptions.IntegrationException;
import br.com.siscov.exceptions.LoginInvalidoException;
import br.com.siscov.model.funcionario.bo.DataAccessConnection;
import br.com.siscov.model.login.dao.LoginDAO;
import br.com.siscov.model.vo.LoginVo;

public class LoginBO extends DataAccessConnection{

	/*public void obterLogin(LoginVo loginVo) throws LoginInvalidoException, IntegrationException {

		LoginDAO loginDAO;
		try {
			loginDAO = new LoginDAO();
			loginDAO.obterLogin(loginVo);

			if (loginVo.getPermissoes().isEmpty()) {
				throw new LoginInvalidoException();
			}
		} catch (Exception e) {
			throw new LoginInvalidoException();
		}

	}*/


	public void excluirLogin(String login) throws IntegrationException {
		LoginDAO loginDAO;
		try {
			loginDAO = new LoginDAO();
			loginDAO.excluirLogin(login);

		} catch (IntegrationException e) {

		}

	}

	public LoginVo obterAcesso(LoginVo loginVo) throws IntegrationException, LoginInvalidoException {
		Connection connection = obterConexao();
		try {
			iniciarTransacao(connection);
			LoginDAO loginDAO = new LoginDAO();
			loginVo = loginDAO.obterAcesso(loginVo);
			confirmarTransacao(connection);
			
		
		} catch (Exception e) {
			throw new IntegrationException(e.getMessage());
		}finally{
			closeConnection(connection);
		}
		return loginVo;
	}

	public boolean autenticaUsuario(String login,String senha) throws Exception{
		Connection connection = obterConexao();
		LoginDAO loginDAO = new LoginDAO();
		boolean retorno;
		try {
			iniciarTransacao(connection);
			retorno=loginDAO.autenticaUsuario(login,senha);
			confirmarTransacao(connection);
		} catch (Exception e) {
			throw new IntegrationException(e.getMessage());
		}finally{
			closeConnection(connection);
		}
		return retorno;		
	}
	
	public void alterarAcesso(LoginVo loginVo) throws IntegrationException {
		Connection connection = obterConexao();
		LoginDAO loginDAO = new LoginDAO();
		
		try {
			iniciarTransacao(connection);
			loginDAO.alterarAcesso(connection, loginVo);
			confirmarTransacao(connection);
		} catch (Exception e) {
			throw new IntegrationException(e.getMessage());
		}finally{
			closeConnection(connection);
		}
			
	}
	
}
